<div class="col-lg-12">
    @include('admin.contents.bar')
    <section class="panel">
        {{ Form::open(array('method' => 'put', 'route' => array('admin.contents.update', $content->contentID),'class'=>'form-horizontal', 'data-validate'=>"parsley")) }}
        <header class="panel-heading">
            <div class="pull-left"><i class="fa fa-edit fa-lg"></i> Edit Content</div>
        </header>
        <div class="panel-body">
            @if ($errors->any())
                <ul class="alert alert-danger">
                    {{ implode('', $errors->all('<p>:message</p>')) }}
                </ul>
            @endif
            <div class="form-group">
                <label class="col-lg-3 control-label">Title</label>
                <div class="col-lg-8">
                    <input type="text" class="bg-focus form-control parsley-validated" data-required="true" placeholder="Title" name="title" value="{{$content->title}}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Category</label>
                <div class="col-lg-8">
                    <select name="categoryID" class="form-control">
                        <option value="">Select Category</option>
                        @foreach($categories as $key => $value)
                            <option value="{{$value->id}}" @if($content->categoryID==$value->id) selected="selected" @endif >{{$value->name}}</option>
                        @endforeach
                    </select>
                </div>
            </div>                
            <div class="form-group">
                <label class="col-lg-3 control-label">Description</label>
                <div class="col-lg-8">
                    <input type="text" class="form-control parsley-validated" data-required="true" placeholder="Description" name="description" value="{{$content->description}}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Content</label>
                <div class="col-lg-8">
                    <textarea data-rangelength="[20,200]" data-trigger="keyup" class="form-control parsley-validated" rows="5" placeholder="Content" name="content">{{$content->content}}</textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Active</label>
                <div class="col-lg-8">
                    <div class="checkbox">
                        <input type="checkbox" name="active" value="Y" @if($content->active=='Y') checked="checked" @endif >
                    </div>
                </div>
            </div>
            <div class="form-group">
                <h5 class="col-lg-3 control-label">SEO</h5>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Meta title</label>
                <div class="col-lg-8">
                    <input type="text" class="form-control parsley-validated" data-required="true" placeholder="Meta Title" name="html_meta_title" value="{{$content->html_meta_title}}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Meta Description</label>
                <div class="col-lg-8">
                    <input type="text" class="form-control parsley-validated" data-required="true" placeholder="Meta Description" name="html_meta_description" value="{{$content->html_meta_description}}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Meta Keywords <small>(keyword,keyword2,keyword3)</small></label>
                <div class="col-lg-8">
                    <input type="text" class="form-control parsley-validated" data-required="true" placeholder="Meta Keyword" name="html_meta_keywords" value="{{$content->html_meta_keywords}}">
                </div>
            </div>
            <div class="form-group">
                <div class="col-lg-9 col-lg-offset-3">
                    <button class="btn btn-white btn-primary" type="submit">Save Content</button>
                    <a class="btn" href="{{ URL::to('admin/contents') }}">Cancel</a>
                </div>
            </div>
        </div>
        {{ Form::close() }}
    </section>
    <section class="panel">
        <a id="content-gallery"></a>
        <header class="panel-heading">
            <i class="fa fa-picture-o fa-lg"></i> Content Gallery  
            @if($gallery != null)    
    		    {{ Form::open(array('route' => array('admin.gallery.destroy', $gallery->id), 'method' => 'delete', "class"=>"pull-right no-margin")) }}
                    <button type="submit" class="btn btn-danger pull-right"><i class="fa fa-trash"></i> Delete Gallery</button>
                {{ Form::close() }}
            @else
    		    {{ Form::open(array('route' => array('admin.gallery.store'), "class"=>"pull-right no-margin")) }}
    		        <input type="hidden" name="objID" value="{{$content->contentID}}">
                    <button type="submit" class="btn btn-info pull-right"><i class="fa fa-plus"></i> Add Gallery</button>
                {{ Form::close() }}
            @endif
        </header>
        @if (Session::has('successGallery') )
            <div class="alert alert-success">
            {{ Session::get('successGallery') }}
            </div>
        @endif
        @if (Session::has('errorGallery') )
            <div class="alert alert-error">
            {{ Session::get('errorGallery') }}
            </div>
        @endif
        @if($gallery != null)    
        <section class="panel-body">
            <button class="btn btn-primary" data-toggle="modal" data-target="#modalImage"><i class="fa fa-camera-retro"></i> Add Images</button>
            <button class="btn btn-primary" data-toggle="modal" data-target="#modalVideo"><i class="fa fa-video-camera"></i> Add Video</button>
            <button class="btn btn-info" id="saveOrder" type="button" style="display:none;">Save Order</button>
            <div class="alert" id="galleryAlert" style="display:none;margin-top:15px;"></div>
            @if(count($galleryItems))
                <br><br>
                <ul class="list-group" id="gallery-order-list">
                   @foreach($galleryItems as $key => $value)
                    <li class="list-group-item" order-item="{{$value['itemID']}}">
                        <div class="pull-left text-success m-t-small">
                            <i class="fa fa-bars text-default"></i>
                        </div>
                        <div class="media" style="padding:0 20px;">
                            <span class="pull-left media-large" style="width:auto;height:100px">{{$value['to_show']}}</span>
                            <div class="pull-right text-success m-t-small">
                                {{ Form::open(array('method' => 'post', 'url' => array('admin/gallery/delete-item', $value['itemID']), "class"=>"pull-right no-margin")) }}
                                    <button type="submit" class="btn btn-danger"><i class="fa fa-trash-o"></i> Delete</button>
                                    <input type="hidden" name="redirectBack" value="{{Request::url()}}#content-gallery">
                                {{ Form::close() }}
                            </div>
                        </div>
                  </li>
                  @endforeach
                </ul>
            @else
                <br><br>
                <div class="alert alert-info">
                    Gallery is empty
                </div>
            @endif
        </section>           
        @endif
    </section>
</div>
@if($gallery != null)    
    @section('modals')
    <div id="modalVideo" class="modal fade">
        {{ Form::open(array('method' => 'post', 'url' => array('admin/gallery/upload-video', $gallery->galleryID), "class"=>"m-b-none")) }}
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><i class="fa fa-times"></i></button>
                        <h4 class="modal-title" id="myModalLabel">Add your video embed code</h4>
                    </div>
                    <div class="modal-body">          
                        <div class="block">
                            <label class="control-label">Embed Code</label>
                            <textarea name="embed_code" class="form-control" placeholder="Content" rows="5"></textarea>
                            <input type="hidden" name="type" value="video">
                            <input type="hidden" name="galleryID" value="{{$gallery->id}}">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-sm btn-primary">Add</button>
                        <button type="button" class="btn btn-sm btn-default" data-dismiss="modal">Cancel</button>
                    </div>
                </div><!-- /.modal-content -->
            </div>
        {{ Form::close() }}
    </div>
    <div id="modalImage" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><i class="fa fa-times"></i></button>
                    <h4 class="modal-title" id="myModalLabel">Add your images</h4>
                </div>
                <div class="modal-body">          
                    @include('admin.fileupload')
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-sm btn-default" data-dismiss="modal">Close</button>
                </div>
            </div><!-- /.modal-content -->
        </div>
    </div>
    @stop
@endif

@section('extra-javascript')
<script> 
$("#gallery-order-list").sortable({
    sort: function(event, ui) {  
        ui.helper.css({'top' : ui.position.top + $(window).scrollTop() + 'px'});
    },
    item:"li",
    update: function(event, ui) {   
        $("#saveOrder").show();
        $("#saveOrder").fadeTo('fast',0.33);
        $("#saveOrder").fadeTo('fast',1);
        $("#saveOrder").fadeTo('fast',0.33);
        $("#saveOrder").fadeTo('fast',1);
        $("#saveOrder").fadeTo('fast',0.33);
        $("#saveOrder").fadeTo('fast',1);
        
   
        $("#saveOrder").click(function() {
            var finish_order = "";
            first = true;
            $('#gallery-order-list li').each(function(){
                if(!first) {
                    finish_order += "&";
                }
                finish_order += "mySortable[]="+$(this).attr('order-item');
                first = false;
            });
        //    resta postear y ordernar
            $.ajax({
                type:"POST",
                url: "{{url('admin/gallery/order-gallery')}}",
                data:{newOrderItems:finish_order},
                success:function(data) {
                    $("#galleryAlert").attr('class','alert');
                    if(data.success==true) {
                        $("#galleryAlert").addClass('alert-success');
                        $("#galleryAlert").html("<p>"+data.message+"</p>");
                    } else {
                        $("#galleryAlert").addClass('alert-danger');
                        $("#galleryAlert").html("<p>"+data.message+"</p>");
                    }
                    $("#galleryAlert").show();
                    $("#galleryAlert").delay(2000).fadeOut(1000);
                }
            });
        });    
    }
});

</script>
@stop
